<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Article extends MY_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */

	public function __construct() {
		parent::__construct();
		 $this->load->model('Category_model', 'c');
		 $this->load->model('Article_model', 'article');
		 $this->load->model('Tag_model','tag');
		 $this->load->model('Tagrelation_model','tr');
		 $this->load->model('Advertisement_model', 'ad');
		// 产品分类
		/*	$data = $this->c->getCateData('production',array());
			$this->_d['pcate'] = cate2list(0, $data);
		 $this->_d['alias'] = 'article';
		 $this->load->model('Advertisement_model', 'ad');
		$this->_d['link'] = $this->ad->getChannelAds('link');
		// 案例分类
		$this->load->model('Article_model', 'article');
		$casecate = $this->c->O(array('alias'=>'case'),'cateid');
		$this->_d['case'] = $this->article->L(array('cateid'=>$casecate['cateid']));*/

	}

	public function index($cateid=0,$total=0)
	{
		$this->load->library('pagination');
		$config['base_url'] = site_url('article/index/'.$cateid.'/'.$total);
		$config['first_tag_open'] = '<li>';
		$config['first_tag_close'] = '</li>';
		$config['last_tag_open'] = '<li>';
		$config['last_tag_close'] = '</li>';
		$config['prev_tag_open'] = '<li>';
		$config['prev_tag_close'] = '</li>';
		$config['next_tag_open'] = '<li>';
		$config['next_tag_close'] = '</li>';
		$config['num_tag_open'] = '<li>';
		$config['num_tag_close'] = '</li>';
		$config['cur_tag_open'] = '<li class="active"><a href="#">';
		$config['cur_tag_close'] = '</a></li>';
		$config['prev_link'] = '上一页';
		$config['next_link'] = '下一页';
		$config['last_link'] = '最后一页';
		$config['first_link'] = '首页';
		$config['reuse_query_string'] = TRUE;
		$sdata['status'] = 1;
		$this->_d['flag'] = 'article';
		//新闻类别
		if ($cateid == '0'){
			$articlecate = $this->c->L(array('func'=>'article',"status"=>'1','alias'=>'article'),'cateid,catename',0,0,'sort');
			foreach ($articlecate as $k => $v) $_cateid[] = $v['cateid'];
			$sdata['cateid in ('.implode(',',$_cateid).')'] = '';
		}else{
			$catedata = $this->c->O(array('cateid'=>$cateid));
			$this->_d['catename'] = $catedata['catename'];
			if ($catedata['alias'] == 'case'){
				$this->_d['flag'] = 'case';
				$this->_d['alias'] = 'article/index/288';
				$articlecate = $this->c->L(array('func'=>'article',"status"=>'1','alias'=>'case','parentid'=>'288'),'cateid,catename',0,0,'sort');
				if ($cateid == '288'){				
					foreach ($articlecate as $k => $v) $_cateid[] = $v['cateid'];
					$sdata['cateid in ('.implode(',',$_cateid).')'] = '';
				}else{
					$sdata['cateid'] = $cateid;
				}
			}else{
				$articlecate = $this->c->L(array('func'=>'article',"status"=>'1','alias'=>'article'),'cateid,catename',0,0,'sort');
				$sdata['cateid'] = $cateid;
			}
		}		
		$offset = intval($this->uri->segment(5));
		$config['per_page'] = 10;
		$article = $this->article->L($sdata,'*',$config['per_page'], $offset,'sort');
		if (!empty($article))
		{
			foreach ($article as $k => $v)
			{
				$article[$k]['attachment'] = $this->getAttach($v['articleid'],'article');
			}
		}
		$config['total_rows'] = $total;
		if (empty($total)) 
		{
			$config['total_rows'] = $this->article->C($sdata);
			$config['base_url'] = site_url('article/index/'.$cateid.'/'.$config['total_rows']);
			
		}
		$config['uri_segment'] = 5;
		$this->pagination->initialize($config);
		$this->_d['page'] = $this->pagination->create_links();
		$this->_d['pagecount'] = $config['total_rows'];
		$this->_d['article'] = $article;
		$this->_d['cateid']=$cateid;
		$this->_d['articlecate'] = $articlecate;
		$this->_d['alias'] = 'article';
		$this->load->view($this->_d['cfg']['tpl'] . 'article', $this->_d);
	}
		
	public function detail($articleid)
	{
		$row = $this->article->O(array('articleid'=>$articleid));
		if (empty($row)) show_404();
		$row['attachment'] = $this->getAttach($articleid,'article');
		$this->_d['row'] = $row;

		$sdata['status'] = 1;
		//新闻类别
		$this->_d['articlecate'] = $this->c->L(array('func'=>'article',"status"=>'1'),'cateid,catename',0,0,'sort');
		$catedata = $this->c->O(array('cateid'=>$row['cateid']));
		if ($catedata['alias'] == 'case') {
			$this->_d['alias'] = 'article/index/288';
			$this->load->view($this->_d['cfg']['tpl'] . 'case', $this->_d);
		}
		else $this->load->view($this->_d['cfg']['tpl'] . 'article_detail', $this->_d);
	}


}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */